<?php
namespace Home\Controller;
use Think\Controller;
class PublicController extends Controller {
	function get_array($array,$obj){
		$return_array=array();
        foreach ($array as $key => $value) {
        	array_push($return_array,$value[$obj]);
        }
        return $return_array;
	}

	//检查是否登录
    function check_login(){
       $url=str_replace('/','%%','http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
       if(!is_login()) $this->error('请登录',U('home/Index/login',array('from'=>$url)));
    }
    //地址三级联动
    function get_address(){
       $id=$_POST['id'];
       $adr=M("address")->where(array("addr_pid"=>$id))->select();
       echo json_encode($adr);
    }
    //登录
    public function doLogin(){
      $name=I('post.username');
      $psd=I('post.userpsd');
      $url=I('post.from');
      //获取登录前的url
      $url=empty($url)?U('Index/index'):str_replace('%%','/',$url);
      $dis=substr(md5(substr($psd,0,2)),0,2);
      $psd.=$dis;
      $result=$this->checkpsd($name,$psd);
      if($result){
        //echo $url;
        redirect($url);
      }else{
        $this->error('账号或密码错误');
      }
      
    }
    //检查密码
    public function checkpsd($name,$psd){
        $code=0;
        $map=array(
          'user_status'=>1,
          'user_name'=>$name
        );
        $data=M('User')->where($map)->find();
        if($data['user_password']===md5($psd)){
          session('uid',$data['user_id']);
          session('username',$name);
          $add=array(
              'user_id'=>$data['user_id'],
              'log_action'=>'login',
              'log_createtime'=>time(),
              'log_ip'=>get_client_ip()
          );
          //add($add,M('log'));
          $code=1;  
        }
        return $code;
    }
    //退出登录
    function outlogin(){
       session('uid',false);
       session('username',false);
       echo session('username');
       $this->error('退出登录','login');
    }
    //分页程序
    function fenpage($count,$row=10,$name='page'){
      $Page       = new \Think\Page($count,$row);// 实例化分页类 传入总记录数和每页显示的记录数(25)
      $show       = $Page->show();// 分页显示输出
      $limit=$Page->firstRow.','.$Page->listRows;
      $this->assign($name,$show);// 赋值分页输出
      return $limit;
    }
    function getPayWay(){
        $payWay=M('PayWay')
              ->where(array('payway_status'=>1))
              ->field(array('payway_name','payway_id'))
              ->select();
        return $payWay;
    }
    function getSendWay(){
        $sendWay=M('SendWay')
              ->where(array('sendway_status'=>1))
              ->field(array('sendway_name','sendway_id'))
              ->select();
        return $sendWay;
    }
    /**
     * 支付宝支付
     * @return [type] [description]
     */
    function alipay(){
          $user = "admin";
          $pass = "admin100";
          $curlPost = "user=$user&pass=$pass";
          $url=$_SERVER['SERVER_NAME'].__ROOT__."/alipay/alipayapi.php";
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
          curl_setopt($ch, CURLOPT_POST, 0);
          curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
          $data = curl_exec($ch);
          curl_close($ch);
    }
    //获取列表
    //function getlist($model,$condition='',$field='*',$raw=10,){
        //$list=M()
    //}
	
}